/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[Example01.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package jdk5.annotation.test;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;

import jdk5.annotation.test.CarColor.Color;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class Example01 {

	/**
	 * 使用反射API解析Annotation
	 * 
	 * @param classType
	 */
	public static void getCarInfo(Class<?> classType) {
		Field[] fields = classType.getDeclaredFields();
		for (Field field : fields) {
			if (field.isAnnotationPresent(CarName.class)) {
				CarName annotation = field.getAnnotation(CarName.class);
				String carName = annotation.value();
				System.out.print("Car Name is " + carName + ", ");
			}
			if (field.isAnnotationPresent(CarColor.class)) {
				CarColor annotation = field.getAnnotation(CarColor.class);
				String carColor = annotation.carColor().toString();
				System.out.print("Car color is " + carColor + ", ");
			}
			if (field.isAnnotationPresent(CarManufacturers.class)) {
				CarManufacturers annotation = field.getAnnotation(CarManufacturers.class);
				String manufacturer = annotation.name() + " and made in " + annotation.country();
				System.out.println("Car manufacturer is " + manufacturer);
			}
		}
	}

	public static void main(String[] args) {
		getCarInfo(Car.class);
	}
}

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 */
class Car {

	@CarName(value = "Audi A4")
	private String carName;

	@CarColor(carColor = Color.BLUE)
	private String carColor;

	@CarManufacturers(name = "Audi", country = "DE")
	private String carManufacturer;
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface CarName {
	String value() default "";
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface CarColor {
	enum Color {
		BLUE, BLACK, GRAY, GREEN, RED, WHITE, YELLOW
	};

	Color carColor() default Color.WHITE;
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface CarManufacturers {
	String name() default "";

	String country() default "";
}